package org.jeecg.modules.yfFangj.service.impl;

import org.jeecg.modules.yfFangj.entity.HqYfFangj;
import org.jeecg.modules.yfFangj.entity.HqYfFangjShes;
import org.jeecg.modules.yfFangj.mapper.HqYfFangjShesMapper;
import org.jeecg.modules.yfFangj.mapper.HqYfFangjMapper;
import org.jeecg.modules.yfFangj.service.IHqYfFangjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 营房房间
 * @Author: jeecg-boot
 * @Date:   2020-06-22
 * @Version: V1.0
 */
@Service
public class HqYfFangjServiceImpl extends ServiceImpl<HqYfFangjMapper, HqYfFangj> implements IHqYfFangjService {

	@Autowired
	private HqYfFangjMapper hqYfFangjMapper;
	@Autowired
	private HqYfFangjShesMapper hqYfFangjShesMapper;
	
	@Override
	@Transactional
	public void saveMain(HqYfFangj hqYfFangj, List<HqYfFangjShes> hqYfFangjShesList) {
		hqYfFangjMapper.insert(hqYfFangj);
		if(hqYfFangjShesList!=null && hqYfFangjShesList.size()>0) {
			for(HqYfFangjShes entity:hqYfFangjShesList) {
				//外键设置
				entity.setZhubId(hqYfFangj.getId());
				hqYfFangjShesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqYfFangj hqYfFangj,List<HqYfFangjShes> hqYfFangjShesList) {
		hqYfFangjMapper.updateById(hqYfFangj);
		
		//1.先删除子表数据
		hqYfFangjShesMapper.deleteByMainId(hqYfFangj.getId());
		
		//2.子表数据重新插入
		if(hqYfFangjShesList!=null && hqYfFangjShesList.size()>0) {
			for(HqYfFangjShes entity:hqYfFangjShesList) {
				//外键设置
				entity.setZhubId(hqYfFangj.getId());
				hqYfFangjShesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqYfFangjShesMapper.deleteByMainId(id);
		hqYfFangjMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqYfFangjShesMapper.deleteByMainId(id.toString());
			hqYfFangjMapper.deleteById(id);
		}
	}
	
}
